/*
 * @Author: 半月狼
 * @Date: 2021-11-09 17:49:37
 * @LastEditor: 半月狼
 * @LastEditTime: 2022-03-08 10:50:32
 * @Description: 
 */
import VConsole from 'vconsole';
import * as commonSDK from 'hybrid-sdk-common';

let openVconsole = function(){
    let openDebugWin = ()=>{
        let debugBox = document.createElement('div');
        debugBox.className = 'debug-box';
        debugBox.innerHTML = `
            <div class="debugbox-title">开启Debug</div>
            <div class="debug-params-box">
                <div class="debugbox-urlinputbox">
                    <span class="url-label">指定URL:</span>
                    <input class="debut-url-input" placeholder="https://...." type="text" value="" />
                </div>
                <div class="debugbox-cookies">
                    <span class="cookies-label">定制Cookies:</span>
                    <div class="cookies-box">
                        
                    </div>
                    <div class="add-cookie">+</div>
                </div>
                <div class="debugbox-vconsolebox">
                    <span class="vconsole-label">开启vconsole:</span>
                    <div class="open-vconsole-wrap off">
                        <span class="open-vconsole-icon"></span>
                    </div>
                </div>
            </div>
            <div class="debug-btns-box">
                <button class="debug-ok">确定</button>
                <button class="debug-cancel">取消</button>
            </div>'
        `;
        document.body.appendChild(debugBox);

        let addCookieDOM = ()=>{
            let cookiesBox = debugBox.querySelector('.cookies-box');

            let cookieItem = document.createElement('div');
            cookieItem.className = 'cookie-item';
            cookieItem.innerHTML = `
                <input class="cookie-key" type="text" />
                <span class="cookievalue-split">:</span>
                <input class="cookie-val" />
            `;
            cookiesBox.appendChild(cookieItem);
        };
        let addCookieBtn = debugBox.querySelector(".add-cookie");
        let openVconsoleBtn = debugBox.querySelector(".open-vconsole-wrap");

        let debugCancel = debugBox.querySelector(".debug-cancel");
        let debugOk = debugBox.querySelector(".debug-ok");

        if(this.vConsole){
            openVconsoleBtn.classList.remove("off");
            openVconsoleBtn.classList.add("on");
            openVconsoleBtn.querySelector(".open-vconsole-icon").style.left = 'auto';
            openVconsoleBtn.querySelector(".open-vconsole-icon").style.right = '1px';
        }

        let addCookieHandler = (e)=>{
            e.stopPropagation();
            e.preventDefault();
            addCookieDOM();
            return;
        };
        let openVconsoleHandler = (e)=>{
            e.stopPropagation();
            e.preventDefault();
            if(openVconsoleBtn.classList.contains("off")){
                openVconsoleBtn.classList.remove("off");
                openVconsoleBtn.classList.add("on");
                openVconsoleBtn.querySelector(".open-vconsole-icon").style.left = 'auto';
                openVconsoleBtn.querySelector(".open-vconsole-icon").style.right = '1px';
                this.vConsole = new VConsole();
            }else{
                openVconsoleBtn.classList.remove("on");
                openVconsoleBtn.classList.add("off");
                openVconsoleBtn.querySelector(".open-vconsole-icon").style.left = '1px';
                openVconsoleBtn.querySelector(".open-vconsole-icon").style.right = 'auto';
                this.vConsole.destroy();
                this.vConsole = null;
            }
            return;
        };
        
        let debugCancelHandler = (e)=>{
            e.stopPropagation();
            e.preventDefault();
            debugBox.parentNode.removeChild(debugBox);
            return;
        };
        let debugOkHandler = (e)=>{
            e.stopPropagation();
            e.preventDefault();
            let _params = {"url": '', "cookies": {}};
            let url = debugBox.querySelector(".debut-url-input").value.replace(/^\s+|\s+$/gm,'');
            let cookies = debugBox.querySelectorAll(".cookie-item");
            let hasCookie = false, hasUrl = false;
            cookies = [].slice.call(cookies);
            if(!!url){
                hasUrl = true;
                _params.url = url;
            }
            cookies.forEach((_cookie)=>{
                let cookieKey = _cookie.querySelector(".cookie-key").value.replace(/^\s+|\s+$/gm,'');
                let cookieVal = _cookie.querySelector(".cookie-val").value.replace(/^\s+|\s+$/gm,'');
                if(!!cookieKey && !!cookieVal){
                    hasCookie = true;
                    _params.cookies[cookieKey] = cookieVal;
                }
            });
            if(commonSDK.terminalCheck.isInApp() && (hasCookie || hasUrl)){
                commonSDK.hybrid('NT_openDebug',  _params, (data)=>{
                    console.log(data);
                    debugBox.parentNode.removeChild(debugBox);
                });
            }else{
                debugBox.parentNode.removeChild(debugBox);
            }
            return;
        };

        addCookieBtn.removeEventListener('click', addCookieHandler, false);
        addCookieBtn.addEventListener('click', addCookieHandler, false);
        
        openVconsoleBtn.removeEventListener('click', openVconsoleHandler, false);
        openVconsoleBtn.addEventListener('click', openVconsoleHandler, false);

        debugCancel.removeEventListener('click', debugCancelHandler, false);
        debugCancel.addEventListener('click', debugCancelHandler, false);
        
        debugOk.removeEventListener('click', debugOkHandler, false);
        debugOk.addEventListener('click', debugOkHandler, false);
        
        addCookieDOM();
    };

    this.util.tclick(document.body, null, null, null, null, null, null, null, (e, startPos)=>{
        if(startPos.half == "right" && startPos.offset.top <= 100 && startPos.offset.right <= 100){
            openDebugWin();
            //this.vConsole = new VConsole();
        }else if(startPos.half == "right" && startPos.offset.bottom <= 100 && startPos.offset.right <= 100){
            openDebugWin();
            //this.vConsole = new VConsole();
        }else{
            //this.vConsole.destroy();
        }
        return;
    }, 11);

    if(commonSDK.terminalCheck.isInApp()){
        commonSDK.hybrid('AI_configWindowFlags',  {"type": 1, "flags": "0x00000080"}, (data)=>{
            // data.result = 1 屏幕常亮设置成功
            //alert(JSON.stringify(data));
        });
    }
}
export default openVconsole;